public static int[][] m_aiMapItemLevel2;
public byte[][] m_abBackGroundLevel2;
public static TiledLayer tlBackGroundLevel2, tlItemLevel2;
int iLevelMapSizeW2=50;
int iLevelMapSizeH2=50;
int iMonsterLevel2=5,iMonsterLeve22=5;
//int iTimeNextLevel=50;
int[] aiOrderDraw2;
boolean bDrawHero2 = true;
public Sprite m_sprMonster2_1, m_sprMonster2_2, m_sprMonster2_3, m_sprMonster2_4, m_sprMonster2_5;
public Monster[] aMonsterLevel2=new Monster[iMonsterLevel2];
public int[] aiItemLevel2 = {7,2,2,10,38,3,1,1,39,3,7,107,40,3,1,1,
							44,27,7,107, 15,18,2,10, 23,26,2,10, 6,21,7,107,
							1,13,2,10,30,38,2,10,
							47,47,7,107};
//s_MainCharacter


void UpdateLevel1(int mode)
{
	if (mode == UpdateType.UPDATE)
	{
	    if (IsKeyPressed(GK_SELECT)) 
			{		
				if(s_MainCharacter.PutBomb(m_aiMapItemLevel2, tlItemLevel2)==1)
				{
					PlaySound("/putBom.wav",1);
				}
			}
	//control main chacracter
		s_MainCharacter.Move(m_aiMapItemLevel2);
		s_MainCharacter.DecreaseBlood(m_aiMapItemLevel2);
		
	//AI for monster		
		for(int i=0; i< aMonsterLevel2.length; i++)
		{	
			if(aMonsterLevel2[i] ==null) continue;
			aMonsterLevel2[i].Move(m_aiMapItemLevel2);
			aMonsterLevel2[i].DecreaseBlood(m_aiMapItemLevel2);
			//monster die
			if(aMonsterLevel2[i].CheckDie())
			{				
				if(--(aMonsterLevel2[i].iTimeDie)>0)
				{					
					aMonsterLevel2[i].m_iDir=4;
					aMonsterLevel2[i].iMove=4;
				}
				else
				{
				m_aiMapItemLevel2[aMonsterLevel2[i].iInMapY][aMonsterLevel2[i].iInMapX]=rand.nextInt(6)+107;
				System.out.println("Item: "+m_aiMapItemLevel2[aMonsterLevel2[i].iInMapY][aMonsterLevel2[i].iInMapX]);
				aMonsterLevel2[i]=null;	
				iMonsterLeve22--;
				}
			}				
		}
		if(iTimeNextLevel<=0) //change to next level
		{
			s_iLevel=2;
			//play sound here
			try
			{
				Thread.sleep(1000);
			}catch(Exception e){}
			SwitchState(GameState.NextLevel);
		}		
	//put boom
		
		if (IsKeyPressed(DK_NUM0))
		{
			s_MainCharacter.ChangeBoom();
		}		
	//check bom explosion
		BombExplosion(m_aiMapItemLevel2, tlItemLevel2);
	
	//set view 
		iViewX=s_MainCharacter.m_iCharacterX-VIEW_W/2;
		iViewY=s_MainCharacter.m_iCharacterY-VIEW_H/2;
		if(iViewX<0)
			iViewX=0;
		else if(iViewX>560)
			iViewX=560;
		if(iViewY<0)
			iViewY=0;
		else if(iViewY>520)
			iViewY=520;
				s_lmMap.setViewWindow(iViewX, iViewY, VIEW_W, VIEW_H);
	
	//check main character die
		if(s_MainCharacter.CheckDie())
		{
			//play sound here
			try
			{
				Thread.sleep(1000);
			}catch(Exception e){}
			if(--s_MainCharacter.s_iLife >0)
				SwitchState(GameState.NextLevel);
			else 
				SwitchState(GameState.GameOver);
		}
		if (IsKeyPressed(DK_SOFT_LEFT))
		SwitchState1(GameState.InGameMenu);
	} //end of Update mode
	
	if (mode == UpdateType.PAINT)
	{		
		s_g.setColor(c_white);
		s_g.fillRect(0, 0, SCREEN_W, SCREEN_H);
	
	//draw background
		s_lmMap.paint(s_g, 0, 0);
		
	//draw monster and main character
		bDrawHero2= true;
		for(int i=0; i< aMonsterLevel2.length; i++)
		{	
		//get depth	
			if(aMonsterLevel2[i] ==null) continue;
			aiOrderDraw2[i]=aMonsterLevel2[i].iDepth;
			aiOrderDraw2[iMonsterLevel2+i]=i;			
		}	
		Sort(aiOrderDraw2);				 
		for(int i=0; i< aMonsterLevel2.length; i++)
		{	
			if(	aMonsterLevel2[aiOrderDraw2[i+iMonsterLevel2]] ==null) continue;	
			if(aMonsterLevel2[aiOrderDraw2[i+iMonsterLevel2]].iDepth > s_MainCharacter.iDepth && bDrawHero2)
			{
				s_MainCharacter.DrawSprite();
				bDrawHero2 = false;
			}
			aMonsterLevel2[aiOrderDraw2[i+iMonsterLevel2]].DrawSprite();
			aMonsterLevel2[aiOrderDraw2[i+iMonsterLevel2]].DrawHeathBar();			
		}
		if(bDrawHero2)
			{
				s_MainCharacter.DrawSprite();
				bDrawHero2 = false;
			}
		
	//check all monster was destroy
	if(iMonsterLeve22<=0)
	{
		iTimeNextLevel--;
		s_g.drawImage(imgWin, m_iMidX, m_iMidY-20, Graphics.HCENTER | Graphics.VCENTER);
	}
	// PaintSoftKey();
	}//end of PAINT mode
	
	if (mode == UpdateType.CTOR)
	{
		try
		{
			iViewX=100;
			iViewY=100;
			//tiledlayer for background
			m_abBackGroundLevel2=ReadFileTiles("/maplevel2.dat", iLevelMapSizeW2, iLevelMapSizeH2);			
			tlBackGroundLevel2 = new TiledLayer(iLevelMapSizeW2, iLevelMapSizeH2, imgInGameTile, 16, 16);		
			if(tlBackGroundLevel2!=null)
				System.out.println("TiledLayer create success !");			
			CreateTiles(m_abBackGroundLevel2, tlBackGroundLevel2, 50, 50);
			
			//For Item
			tlItemLevel2 = new TiledLayer(iLevelMapSizeW2, iLevelMapSizeH2, imgItemTile, 16, 16);	
			//1-5: soft-wall
			//6-12 Item
			//13-17 Bom
			//17-25 bom explosion
			//26-30 hit
			m_aiMapItemLevel2=new int[50][50];
			CreateMapItem(m_abBackGroundLevel2, m_aiMapItemLevel2);
		//add default Item
			AddMapItemLevel2(m_aiMapItemLevel2, tlItemLevel2);
			
			 System.out.println("Initialize NPC...");
			// Initialize Character 		
			
		//Create monster				
			if(monsterImage[0] == null)System.out.println("Error load img for monster");
			//m_sprMonster1 = new Sprite(monsterImage[0], 32, 32);			
			aMonsterLevel2[0]= new Monster(monsterImage[2], 24, 24);
			aMonsterLevel2[1]= new Monster(monsterImage[1], 24, 24);
			aMonsterLevel2[2]= new Monster(monsterImage[1], 24, 24);	
			aMonsterLevel2[3]= new Monster(monsterImage[1], 24, 24);	
			aMonsterLevel2[4]= new Monster(monsterImage[2], 24, 24);	
			
			aMonsterLevel2[0].SetPosition(37,47);
			aMonsterLevel2[1].SetPosition(22,44);
			aMonsterLevel2[2].SetPosition(5,9);
			aMonsterLevel2[2].m_iAI=2;
			aMonsterLevel2[3].SetPosition(47,5);
			aMonsterLevel2[4].SetPosition(39,44);
						
			aMonsterLevel2[1].m_iBlood = 2;
			// aMonsterLevel1[1].m_iAI = 2;
			aMonsterLevel2[2].m_iBlood = 2;
			aMonsterLevel2[3].m_iBlood = 2;
			aMonsterLevel2[4].m_iBlood = 2;
			// s_lmMap.append(s_MainCharacter);
			// for(int i=0;i<iMonsterLevel2;i++)
		    // s_lmMap.append(aMonsterLevel2[i]);
		    
		    aiOrderDraw2 = new int[iMonsterLevel2*2];
			s_lmMap.append(tlItemLevel2);
			s_lmMap.append(tlBackGroundLevel2);
		} 
		catch (Exception e)
		{
			System.out.println("Error Level1-CTOR: " + e.getMessage());
		}
				// try{Thread.sleep(21000);}catch (Exception e)	{}				
		
	}
	if (mode== UpdateType.DTOR)
	{	
		// switch(s_iLevel)
		// {
			// case 1: break;
			// case 2: break;
			// case 3: UpdateLevel3(mode); break;			
		// }
		for(int i=0;i<iMonsterLevel2;i++)aMonsterLevel2[i]=null;
		tlItemLevel2=null;
		tlBackGroundLevel2=null;
		for(int i=0;i<iMonsterLevel2;i++)s_lmMap.remove(aMonsterLevel2[i]);
		s_lmMap.remove(tlItemLevel2);
		s_lmMap.remove(tlBackGroundLevel2);
	}		
}


// public void BombExplosion(int[][] data, TiledLayer tl) // display bom explosion and Item
// {
	// int h= data.length;
	// int w= data[0].length;
	// for(int i =0; i< h; i++)
		// for(int j=0; j<w; j++)
		// {
		//has bom in this cell
			// if((data[i][j]>BOMB)) //>1000
				// if((data[i][j]%200) != 100) // bom in time
					// data[i][j]-=2;
				// else //bom explosion
				// {					
					// System.out.println("bum`... !"); //play sound here
					// PlaySound("/explosion.wav",1);
					// CreateBombExplosion((int)(Math.ceil((data[i][j]-BOMB)/200.0)), j, i, data, tl);					
				// }				
		//has explosion of bom
			// else if((data[i][j]>HIT_B) && (data[i][j]<=HIT_A)) //>500 && <=700
			// {
				// if(data[i][j] > EXPLOSION_A) //650--700
				// {					
					// tl.setCell(j,i,18+(HIT_A - data[i][j])/7);
					//System.out.println("Img:" +((HIT_A - data[i][j])/7) + "  %" +data[i][j] );
					// data[i][j]-=7;
				// }
				// else if(data[i][j] > EXPLOSION_B && data[i][j] <=HIT_C) //550-600
				// {
					// data[i][j]-=1;
					// tl.setCell(j,i,26+(HIT_C-data[i][j])%4);
				// }
				// else 
				// {
					// data[i][j]=20;
					//tl.setCell(j,i,0);
				// }
			// }
			// else if(data[i][j]>100 && data[i][j]<=300)//Item
			// {
				// tl.setCell(j,i,data[i][j]-100);
			// }
			// else if(data[i][j]==FREE_CELL)
					// tl.setCell(j,i,0);
		// }
// }
// public void CreateBombExplosion(int kind, int x, int y, int[][] data, TiledLayer tl)
// {
	// int power=s_MainCharacter.iPower;
	// Random rand = new Random();	
	//System.out.println("130/200= "+(int)(130/200.0));
	
	// if(kind ==1)
	// {
		// s_MainCharacter.iNumBom++; //after bom explosion -> main char will can put another boom
		// data[y][x]=HIT_A;
		//tl.setCell(x,y,26);	
		// for(int i=x; i<(power+x); i++) //explosion right
			// {
				// if(data[y][i]<0)break;							
				// if(data[y][i] < FREE_CELL && data[y][i] > 0) //destroy soft item
				// {
					// if(data[y][i]<10)
						// data[y][i]=FREE_CELL;
					// else 
						// data[y][i]= rand.nextInt(6) + 107; //create item 
					// break;	
				// }
				// else if(data[y][i]>=FREE_CELL && data[y][i] < BOMB) //create explosion
				// {
					// data[y][i]=HIT_A;
					//tl.setCell(i,y,26);
				// }
				// else if(data[y][i]>BOMB) CreateBombExplosion((int)(Math.ceil((data[y][i]-BOMB)/200.0)), i, y, data, tl);
			// }
		// for(int i=x; i>x-power; i--) //explosion left
			// {
				// if(data[y][i]<0)break;
				// if(data[y][i] < FREE_CELL && data[y][i] > 0) //destroy soft item
				// {
					// if(data[y][i]<10)
						// data[y][i]=FREE_CELL;
					// else 
						// data[y][i]= rand.nextInt(6) + 107; //create item 
					// break;	
				// }
				// else if(data[y][i]>=FREE_CELL && data[y][i] < BOMB) //create explosion
				// {
					// data[y][i]=HIT_A;
					//tl.setCell(i,y,26);
				// }
				// else if(data[y][i]>BOMB) CreateBombExplosion((int)(Math.ceil((data[y][i]-BOMB)/200.0)), i, y, data, tl);
			// }
		// for(int i=y; i>(y-power); i--) //explosion up
			// {
				// if(data[i][x]<0)break;
				// if(data[i][x] < FREE_CELL && data[i][x] > 0) //destroy soft item
				// {
					// if(data[i][x]<10)
						// data[i][x]=FREE_CELL;
					// else 
						// data[i][x]= rand.nextInt(6) + 107; //create item 
					// break;	
				// }
				// else if(data[i][x]>=FREE_CELL && data[i][x] < BOMB) //create explosion
				// {
					// data[i][x]=HIT_A;
					//tl.setCell(x,i,26);
				// }
				// else if(data[i][x]>BOMB) CreateBombExplosion((int)(Math.ceil((data[i][x]-BOMB)/200.0)), x, i, data, tl);
			// }
		// for(int i=y; i<(y+power); i++) //explosion down
			// {
				// if(data[i][x]<0)break;
				// if(data[i][x] < FREE_CELL && data[i][x] > 0) //destroy soft item
				// {
					// if(data[i][x]<10)
						// data[i][x]=FREE_CELL;
					// else 
						// data[i][x]= rand.nextInt(6) + 107; //create item
					// break;	
				// }
				// else if(data[i][x]>=FREE_CELL && data[i][x] < BOMB) //create explosion
				// {
					// data[i][x]=HIT_A;
					//tl.setCell(x,i,26);
				// }
				// else if(data[i][x]>BOMB) CreateBombExplosion((int)(Math.ceil((data[i][x]-BOMB)/200.0)), x, i, data, tl);
			// }
	// } //end kind 1
	
	// if(kind ==2)
	// {
		// s_MainCharacter.iNumBom++; //after bom explosion -> main char will can put another boom
		// data[y][x]=HIT_C;
		//tl.setCell(x,y,26);	
		// for(int i=x; i<(power+x); i++) //explosion right
			// {
				// if(data[y][i]<0)break;							
				// if(data[y][i] < FREE_CELL && data[y][i] > 0) //destroy soft item
				// {
					// if(data[y][i]<10)
						// data[y][i]=FREE_CELL;
					// else 
						// data[y][i]= rand.nextInt(6) + 107; //create item 
					// break;	
				// }
				// else if(data[y][i]>=FREE_CELL && data[y][i] < BOMB) //create explosion
				// {
					// data[y][i]=HIT_C;
					//tl.setCell(i,y,26);
				// }
				// else if(data[y][i]>BOMB) CreateBombExplosion((int)(Math.ceil((data[y][i]-BOMB)/200.0)), i, y, data, tl);
			// }
		// for(int i=x; i>x-power; i--) //explosion left
			// {
				// if(data[y][i]<0)break;
				// if(data[y][i] < FREE_CELL && data[y][i] > 0) //destroy soft item
				// {
					// if(data[y][i]<10)
						// data[y][i]=FREE_CELL;
					// else 
						// data[y][i]= rand.nextInt(6) + 107; //create item 
					// break;	
				// }
				// else if(data[y][i]>=FREE_CELL && data[y][i] < BOMB) //create explosion
				// {
					// data[y][i]=HIT_C;
					//tl.setCell(i,y,26);
				// }
				// else if(data[y][i]>BOMB) CreateBombExplosion((int)(Math.ceil((data[y][i]-BOMB)/200.0)), i, y, data, tl);
			// }
		// for(int i=y; i>(y-power); i--) //explosion up
			// {
				// if(data[i][x]<0)break;
				// if(data[i][x] < FREE_CELL && data[i][x] > 0) //destroy soft item
				// {
					// if(data[i][x]<10)
						// data[i][x]=FREE_CELL;
					// else 
						// data[i][x]= rand.nextInt(6) + 107; //create item 
					// break;	
				// }
				// else if(data[i][x]>=FREE_CELL && data[i][x] < BOMB) //create explosion
				// {
					// data[i][x]=HIT_C;
					//tl.setCell(x,i,26);
				// }
				// else if(data[i][x]>BOMB) CreateBombExplosion((int)(Math.ceil((data[i][x]-BOMB)/200.0)), x, i, data, tl);
			// }
		// for(int i=y; i<(y+power); i++) //explosion down
			// {
				// if(data[i][x]<0)break;
				// if(data[i][x] < FREE_CELL && data[i][x] > 0) //destroy soft item
				// {
					// if(data[i][x]<10)
						// data[i][x]=FREE_CELL;
					// else 
						// data[i][x]= rand.nextInt(6) + 107; //create item 
					// break;	
				// }
				// else if(data[i][x]>=FREE_CELL && data[i][x] < BOMB) //create explosion
				// {
					// data[i][x]=HIT_C;
					//tl.setCell(x,i,26);
				// }
				// else if(data[i][x]>BOMB) CreateBombExplosion((int)(Math.ceil((data[i][x]-BOMB)/200.0)), x, i, data, tl);
			// }
	// } //end kind 2
// }

public void AddMapItemLevel2(int [][] data, TiledLayer tl)
	{
		int l=aiItemLevel2.length/4;
		for(int i=0; i<l; i++)
		{
			data[aiItemLevel2[i*4+1]][aiItemLevel2[i*4]] = aiItemLevel2[i*4+3];		
			tl.setCell(aiItemLevel2[i*4],aiItemLevel2[i*4+1], aiItemLevel2[i*4+2]);				
			System.out.println(aiItemLevel2[i*4] +":" +aiItemLevel2[i*4+1] +":" +aiItemLevel2[i*4+2] +":" +aiItemLevel2[i*4+3]);
		}
		System.out.println("Created default item !");
	}
// public void DrawSoftItem(int data, int x, int y)
// {
	// s_g.drawImage(aimgSoftItem[data], x*16, y*16, Graphics.TOP|Graphics.LEFT);
// }
// public boolean a()
// {
	// System.out.println("Em vao day roi");
	// return true;
// }
